﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace Client
{
    public partial class FormVipAudit : BaseChildForm
    {
        QunYing.BLL.VipInfo bll_vipInfo= new QunYing.BLL.VipInfo();
        QunYing.BLL.ICSA_Device bll_icsa = new QunYing.BLL.ICSA_Device();
        QunYing.BLL.VipRank bll_VipRank = new QunYing.BLL.VipRank();
        QunYing.BLL.VipPayOrder bll_pay = new QunYing.BLL.VipPayOrder();
        QunYing.BLL.UserLoginInfo bll_LoginInfo = new QunYing.BLL.UserLoginInfo();

        public FormVipAudit()
        {
            InitializeComponent();
            this.panelControl1.Controls.Add(panel_btn);
           this.sharpGrid1.RowDoubleClickEvent += new EventHandler<Spore.Controls.RowDoubleClickEventArg>(sharpGrid1_RowDoubleClickEvent);
           this.BindData();
        }

        void sharpGrid1_RowDoubleClickEvent(object sender, Spore.Controls.RowDoubleClickEventArg e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            foreach (var v in selectedRows)
            {
                if (v >= 0)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;
                    QunYing.Model.VipInfo vi = this.bll_vipInfo.GetModel(row.Row["会员卡号"].ToString());
                    VipLook form = SingleObjManager.GetSingleObj<VipLook>();
                    form.BindVipInfo(vi);
                    form.ShowDialog();
                    break;
                }
            }
        }

        public override void  BindData()
        {
           this.sharpGrid1.DataSource=bll_vipInfo.GetList(" 审核状态=\'未审核\'").Tables[0];
           ((GridView)this.sharpGrid1.MainView).Columns["地址图片"].Visible = false;
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            if (!this.DesignMode && this.panel_btn != null)
            {
                if(this.panel_btn.Parent!=null)
                this.panel_btn.Left = (this.panel_btn.Parent.Width - this.panel_btn.Width) / 2;
            }
            base.OnSizeChanged(e);
        }

        private void btn_审核通过_Click(object sender, EventArgs e)
        {
            try
            {
                GridView view = this.sharpGrid1.MainView as GridView;
                int[] selectedRows = view.GetSelectedRows();
                foreach (var v in selectedRows)
                {
                    if (v >= 0)
                    {
                        object obj = view.GetRow(v);
                        DataRowView row = obj as DataRowView;
                        if (row == null)
                            continue;
                        QunYing.Model.VipInfo model = bll_vipInfo.GetModel(row.Row["会员卡号"].ToString());
                        QunYing.BLL.DeviceInfo deviceInfo = new QunYing.BLL.DeviceInfo();
                        if (model != null)
                        {
                            model.审核状态 = "审核通过";
                            bll_vipInfo.Update(model);
                            string[] deviceID = model.水族箱设备ID.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                            foreach (var dv in deviceID)
                            {
                                var vv = bll_icsa.GetModel(dv);
                                if (vv != null)
                                {
                                        QunYing.Model.DeviceInfo dif = new QunYing.Model.DeviceInfo();
                                        dif.ICSA编号 = dv;
                                        dif.名称 = dv + "鱼缸";
                                        dif.品牌 = "未设置";
                                        dif.水草品种 = "未设置";
                                        dif.水族箱地址 = "未设置";
                                        dif.箱体尺寸 = "未设置";
                                        dif.型号 = "未设置";
                                        dif.鱼类品种 = "未设置";
                                         vv.水族箱ID=deviceInfo.Add(dif);
                                        vv.报警接收手机号码 = model.移动电话;
                                         vv.是否已售 = true;
                                         vv.所属经销商 = model.经销商编号;
                                        bll_icsa.Update(vv);
                                }
                            }
                            QunYing.Model.UserLoginInfo userLoginInfo = new QunYing.Model.UserLoginInfo();
                            userLoginInfo.角色 = "会员";
                            userLoginInfo.密码 = "123456";
                            userLoginInfo.手机号码 = model.移动电话;
                            userLoginInfo.用户ID = model.用户ID;
                            userLoginInfo.电邮地址 = model.电邮地址;
                            bll_LoginInfo.Add(userLoginInfo);
                            var ranks = bll_VipRank.GetModelList(string.Format(@"等级名称='{0}'", model.会员级别));
                            int year = ((model.入会时间 - model.到期时间).Days) / 360;
                            QunYing.Model.VipPayOrder userPay = new QunYing.Model.VipPayOrder();
                            userPay.缴费金额 = model.首次缴费.Value;
                            userPay.会员新级别 = model.会员级别;
                            userPay.会员原级别 = model.会员级别;
                            userPay.原服务选项 = model.所选服务项;
                            userPay.原到期时间 = model.入会时间.ToString();
                            userPay.新服务选项 = model.所选服务项;
                            userPay.经销商ID = model.经销商编号;
                            userPay.提交时间 = model.入会时间;
                            userPay.新到期时间 = model.到期时间;
                            userPay.审核时间 = DateTime.Now;
                            userPay.审核状态 = "审核通过";
                            userPay.审核人 = GlobalInfo.UserLoginInfo.用户ID;
                            userPay.会员卡号 = model.会员卡号;
                            bll_pay.Add(userPay);
                            row["审核状态"] = "审核通过";
                          
                          
                            GlobalInfo.MsgBoxShowInfo(string.Format("会员 {0}  审核工作已完成！", model.会员卡号));
                            this.BindData();
                        }
                        break;
                    }

                }
            }
            catch (Exception ex)
            {
                GlobalInfo.MsgBoxShowError("审核会员时出现错误："+ex.Message);
            }
        }

        private void btn_不予通过_Click(object sender, EventArgs e)
        {
           GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            foreach (var v in selectedRows)
            {
                if (v >= 0)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;
                    QunYing.Model.VipInfo model = bll_vipInfo.GetModel(row.Row["会员卡号"].ToString());
                    if (model != null)
                    {
                        model.审核状态 = "审核不通过";
                        bll_vipInfo.Update(model);
                        foreach (var dID in model.水族箱设备ID.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                        {
                            var device = bll_icsa.GetModel(dID);
                            if (device != null)
                            {
                                device.是否已售 = false;
                                bll_icsa.Update(device);
                            }
                        }
                        GlobalInfo.MsgBoxShowInfo(string.Format("会员 {0}  审核工作已完成！", model.会员卡号));
                        break;
                    }
                }
            }
        }
    }
}